package com.wanjia.estate.service.httpservice;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wanjia.estate.exception.HttpRequestException;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
public abstract class AbstractHttpService {

    @Setter(onMethod_ = @Autowired)
    protected RestTemplate template;

    public static <T> T getBody(ResponseEntity<T> entity) {
        if (entity == null) {
            throw new HttpRequestException("响应实体不能为空");
        }

        T body = entity.getBody();
        if (!entity.getStatusCode().is2xxSuccessful()) {
            throw new HttpRequestException(JSONUtil.toJsonStr(body));
        }

        return body;
    }

    protected <T> T parseResponseEntity(ResponseEntity<T> entity) {
        return getBody(entity);
    }

    /**
     * 调用Hutool解析响应json
     *
     * @param entity 请求响应
     * @param c      响应结果目标类型
     * @param <T>    响应结果目标类型泛型
     * @return 响应结果
     */
    protected <T> T parseObjResponseEntity(ResponseEntity<String> entity, Class<T> c) {
        String bodyStr = getBody(entity);
        log.debug("response body str: {}", bodyStr);
        return JSONUtil.toBean(bodyStr, JSONConfig.create().setIgnoreCase(true), c);
    }

    /**
     * 调用Hutool解析响应json
     *
     * @param entity 请求响应
     * @param c      响应结果目标类型
     * @param <T>    响应结果目标类型泛型
     * @return 响应结果
     */
    protected <T> List<T> parseListResponseEntity(ResponseEntity<String> entity, Class<T> c) {
        String bodyStr = getBody(entity);
        log.debug("response body str: {}", bodyStr);
        return JSONUtil.parseArray(bodyStr, JSONConfig.create().setIgnoreCase(true)).toList(c);
    }

    /**
     * 调用Fastjson解析响应json
     *
     * @param entity 请求响应
     * @param c      响应结果目标类型
     * @param <T>    响应结果目标类型泛型
     * @return 响应结果
     */
    protected <T> T parseObjResponseEntity2(ResponseEntity<String> entity, Class<T> c) {
        String bodyStr = getBody(entity);
        log.debug("response body str: {}", bodyStr);
        return JSON.parseObject(bodyStr, c);
    }

    /**
     * 调用Fastjson解析响应json
     *
     * @param entity 请求响应
     * @param c      响应结果目标类型
     * @param <T>    响应结果目标类型泛型
     * @return 响应结果
     */
    protected <T> List<T> parseListResponseEntity2(ResponseEntity<String> entity, Class<T> c) {
        String bodyStr = getBody(entity);
        log.debug("response body str: {}", bodyStr);
        return JSON.parseArray(bodyStr, c);
    }

    /**
     * 实体类转换为xml
     * @param entity 实体类
     * @return xml字符串
     * @param <T> 实体类类型泛型
     */
    protected <T> String toXml(T entity) {
        String xml = JSONUtil.toXmlStr(JSONUtil.parse(JSON.toJSONString(entity,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullListAsEmpty)));
        log.debug("request body xml: {}", xml);
        return xml;
    }

    /**
     * xml转换为实体类
     * @param xml xml字符串
     * @param c 实体类类型
     * @return 实体类
     * @param <T> 实体类类型泛型
     */
    protected <T> T fromXml(String xml, Class<T> c) {
        return JSON.parseObject(JSONUtil.xmlToJson(xml).toString(), c);
    }
}
